package com.zim.fibonaccinative;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends Activity {

	private TextView output;
	private EditText input;
	private RadioGroup type;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		output = (TextView) findViewById(R.id.output);
		input = (EditText) findViewById(R.id.input);
		type = (RadioGroup) findViewById(R.id.type);

		Button action = (Button) findViewById(R.id.action);
		action.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				String s = input.getText().toString();
				if (TextUtils.isEmpty(s)) {
					return;
				}
				final ProgressDialog dial = ProgressDialog.show(
						MainActivity.this, "Fib in Progress", "Calculating",
						true);

				final long n = Long.parseLong(s);

				new AsyncTask<Void, Void, String>() {

					@Override
					protected String doInBackground(Void... params) {
						long result = 0;
						long t = System.currentTimeMillis();
						switch (type.getCheckedRadioButtonId()) {
						case R.id.fibJI:
							result = FibLib.fibJI(n);
							break;
						case R.id.fibJR:
							result = FibLib.fibJR(n);
							break;

						case R.id.fibNI:
							result = FibLib.fibNI(n);
							break;
						case R.id.fibNR:
							result = FibLib.fibNR(n);
							break;

						default:
							break;
						}

						t = System.currentTimeMillis() - t;
						return String.format("fib(%d)=%d in %d ms", n, result,
								t);
					}

					protected void onPostExecute(String result) {
						dial.dismiss();
						output.setText(result);
					}
				}.execute();

			}
		});
	}

}
